package com.service.gmall.product.api;

import com.service.gmall.common.result.Result;
import com.service.gmall.model.product.SkuImage;
import com.service.gmall.model.product.SkuInfo;
import com.service.gmall.model.product.SpuSaleAttr;
import com.service.gmall.model.to.CategoryViewTo;
import com.service.gmall.product.service.BaseCategory3Service;
import com.service.gmall.product.service.SkuInfoService;
import com.service.gmall.product.service.SpuSaleAttrService;
import jdk.internal.util.xml.impl.ReaderUTF8;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author alpha
 * @className: SkuDetailApiController
 * @date 2022/8/26 15:16
 * @Description :商品详情数据库层操作
 */
@RestController
@RequestMapping("/api/inner/rpc/product")
public class SkuDetailApiController {

    @Autowired
    SkuInfoService skuInfoService;
    @Autowired
    SpuSaleAttrService spuSaleAttrService;
    @Autowired
    BaseCategory3Service baseCategory3Service;
//    /**
//     * @param
//     * @description: 查询商品页详情
//     * @return: com.service.gmall.common.result.Result<com.service.gmall.model.to.SkuDetailsTo>
//     * @author: alpha
//     * @date: 2022/8/26 22:38
//     */
//    @GetMapping("/getSkuDetails/{skuId}")
//    public Result<SkuDetailsTo> getSkuDetails(@PathVariable("skuId") Long skuId) {
//        SkuDetailsTo skuDetailsTo = skuInfoService.getSkuDetails(skuId);
//        return Result.ok(skuDetailsTo);
//    }

    /**
     * 根据三级分类ID查询分类
     * @param category3Id
     * @return
     */
    @GetMapping("/skuDetail/getCategoryView/{category3Id}")
    public Result<CategoryViewTo> getCategoryView(@PathVariable("category3Id") Long category3Id){
        CategoryViewTo categoryViewTo = baseCategory3Service.getCategoryView(category3Id);
        return Result.ok(categoryViewTo);
    }

    /**
     * 根据skuId查询sku基本信息
     * @param skuId
     * @return
     */
    @GetMapping("/skuDetail/info/{skuId}")
    public Result<SkuInfo> getSkuInfo(@PathVariable("skuId") Long skuId) {
        SkuInfo skuInfo = skuInfoService.getSkuInfo(skuId);
        return Result.ok(skuInfo);
    }

    /**
     * 获取sku的所有图片信息
     * @param skuId
     * @return
     */
    @GetMapping("/skuDetail/images/{skuId}")
    public Result<List<SkuImage>> getSkuImages(@PathVariable("skuId") Long skuId){
        List<SkuImage> images =skuInfoService.getDetailImages(skuId);

        return Result.ok(images);
    }

    /**
     * 根据skuId获取实时价格
     * @param skuId
     * @return
     */
    @GetMapping("/skuDetail/price/{skuId}")
    public Result<BigDecimal> getSkuRealTimePrice(@PathVariable("skuId") Long skuId){
        BigDecimal realTimePrice = skuInfoService.getRealTimePrice(skuId);
        return Result.ok(realTimePrice);
    }

    /**
     * 查询销售属名和值
     * @param spuId
     * @param skuId
     * @return
     */
    @GetMapping("/skuDetail/getSpuSaleAttrList/{spuId}/{skuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttrList(@PathVariable("spuId") Long spuId,
                                     @PathVariable("skuId")Long skuId){
        List<SpuSaleAttr> saleAttrList = spuSaleAttrService.getSaleAttrAndValueMarkSku(spuId,skuId);
        return Result.ok(saleAttrList);
    }

    /**
     * 查询sku组合
     * @param spuId
     * @return
     */
    @GetMapping("/skuDetail/getValuesSkuJson/{spuId}")
    public Result<String> getValuesSkuJson(@PathVariable("spuId") Long spuId){
        String valueJson=spuSaleAttrService.getAllSkuSaleAttrValueJson(spuId);
        return Result.ok(valueJson);
    }
}
